from models.payment import PaymentMethod
from models.user import User
from models.product import Product


class Transaction:
    def __init__(self, user: User, page: str, product: Product, 
                 quantity: int, payment_method: PaymentMethod):
        self.user = user
        self.page = page
        self.product = product
        self.quantity = quantity
        self.payment_method = payment_method
        self.success = False
        self.total_cost = self.product.get_cost(payment_method) * quantity

    def process(self) -> bool:
        """处理交易逻辑。默认交易成功，若为积分且花费超200则标记用户需关怀。"""
        self.success = True

        if self.payment_method == PaymentMethod.POINTS and self.total_cost > 200:
            self.user.mark_need_care()

        return self.success

    def __str__(self):
    # 商品单位映射
        unit_map = {
            "摄影套餐": "套",
            "猫粮": "袋",
            "宠物养护服务": "次",
            "体验套餐": "次"
        }
    
        # 数量中文映射
        quantity_map = {
            1: "一",
            2: "两",
            3: "三"
        }
        
        # 支付方式描述
        payment_desc = {
            PaymentMethod.BALANCE: "余额",
            PaymentMethod.CASH: "现金",
            PaymentMethod.POINTS: "积分",
            PaymentMethod.COUPON: "优惠券"
        }
        
        # 获取商品单位和数量中文
        unit = unit_map.get(self.product.name, "份")
        quantity_cn = quantity_map.get(self.quantity, str(self.quantity))
        
        # 构建基础交易信息
        transaction_info = (
            f"{self.user.name}在{self.page}使用{payment_desc[self.payment_method]}"
            f"{'购买' if self.payment_method != PaymentMethod.POINTS else '兑换'}了"
            f"{quantity_cn}{unit}{self.product.name}"
        )
        
        # 添加花费信息
        if self.payment_method == PaymentMethod.COUPON:
            cost_info = f"，花费{int(self.total_cost)}张"
        elif self.payment_method == PaymentMethod.POINTS:
            cost_info = f"，花费{int(self.total_cost)}积分"
        else:
            cost_info = f"，花费{int(self.total_cost)}块"
        
        transaction_info += cost_info
        
        # 添加特殊备注
        if self.payment_method == PaymentMethod.POINTS and self.total_cost >= 300:
            transaction_info += "\n疑似潜在脱坑客户，需要关怀"
        
        return transaction_info
